import React, { useRef, useEffect, useState } from 'react';
import ReactECharts from 'echarts-for-react';
import chinaJson from './china.json'; // 确保路径正确
import * as echarts from 'echarts';
import { throttle } from 'lodash'
interface ChinaMapProps {
  option?: any;
  width?: string;
  height?: string;
}
echarts.registerMap('china', chinaJson as any);
var geoCoordMap:any = {
  新疆玛纳斯基地: [86.22, 44.3],
  九江: [116.0, 29.7],
  新乡: [116.402217, 35.311657],
  若羌县: [88.17, 39.02],
  上海: [121.4648, 31.2891],
  东莞: [113.8953, 22.901],
  东营: [118.7073, 37.5513],
  中山: [113.4229, 22.478],
  临汾: [111.4783, 36.1615],
  临沂: [118.3118, 35.2936],
  丹东: [124.541, 40.4242],
  丽水: [119.5642, 28.1854],
  乌鲁木齐: [87.9236, 43.5883],
  佛山: [112.8955, 23.1097],
  保定: [115.0488, 39.0948],
  兰州: [103.5901, 36.3043],
  包头: [110.3467, 41.4899],
  北京: [116.4551, 40.2539],
  北海: [109.314, 21.6211],
  南京: [118.8062, 31.9208],
  南宁: [108.479, 23.1152],
  南昌: [116.0046, 28.6633],
  南通: [121.1023, 32.1625],
  厦门: [118.1689, 24.6478],
  台州: [121.1353, 28.6688],
  合肥: [117.29, 32.0581],
  呼和浩特: [111.4124, 40.4901],
  咸阳: [108.4131, 34.8706],
  哈尔滨: [127.9688, 45.368],
  唐山: [118.4766, 39.6826],
  嘉兴: [120.9155, 30.6354],
  大同: [113.7854, 39.8035],
  大连: [122.2229, 39.4409],
  天津: [117.4219, 39.4189],
  太原: [112.3352, 37.9413],
  威海: [121.9482, 37.1393],
  宁波: [121.5967, 29.6466],
  宝鸡: [107.1826, 34.3433],
  宿迁: [118.5535, 33.7775],
  常州: [119.4543, 31.5582],
  广州: [113.5107, 23.2196],
  廊坊: [116.521, 39.0509],
  延安: [109.1052, 36.4252],
  张家口: [115.1477, 40.8527],
  徐州: [117.5208, 34.3268],
  德州: [116.6858, 37.2107],
  惠州: [114.6204, 23.1647],
  成都: [103.9526, 30.7617],
  扬州: [119.4653, 32.8162],
  承德: [117.5757, 41.4075],
  拉萨: [91.1865, 30.1465],
  无锡: [120.3442, 31.5527],
  日照: [119.2786, 35.5023],
  昆明: [102.9199, 25.4663],
  杭州: [119.5313, 29.8773],
  枣庄: [117.323, 34.8926],
  柳州: [109.3799, 24.9774],
  株洲: [113.5327, 27.0319],
  武汉: [114.3896, 30.6628],
  汕头: [117.1692, 23.3405],
  江门: [112.6318, 22.1484],
  沈阳: [123.1238, 42.1216],
  沧州: [116.8286, 38.2104],
  河源: [114.917, 23.9722],
  泉州: [118.3228, 25.1147],
  泰安: [117.0264, 36.0516],
  泰州: [120.0586, 32.5525],
  济南: [117.1582, 36.8701],
  济宁: [116.8286, 35.3375],
  海口: [110.3893, 19.8516],
  淄博: [118.0371, 36.6064],
  淮安: [118.927, 33.4039],
  深圳: [114.5435, 22.5439],
  清远: [112.9175, 24.3292],
  温州: [120.498, 27.8119],
  渭南: [109.7864, 35.0299],
  湖州: [119.8608, 30.7782],
  湘潭: [112.5439, 27.7075],
  滨州: [117.8174, 37.4963],
  潍坊: [119.0918, 36.524],
  烟台: [120.7397, 37.5128],
  玉溪: [101.9312, 23.8898],
  珠海: [113.7305, 22.1155],
  盐城: [120.2234, 33.5577],
  盘锦: [121.9482, 41.0449],
  石家庄: [114.4995, 38.1006],
  福州: [119.4543, 25.9222],
  秦皇岛: [119.2126, 40.0232],
  绍兴: [120.564, 29.7565],
  聊城: [115.9167, 36.4032],
  肇庆: [112.1265, 23.5822],
  舟山: [122.2559, 30.2234],
  苏州: [120.6519, 31.3989],
  莱芜: [117.6526, 36.2714],
  菏泽: [115.6201, 35.2057],
  营口: [122.4316, 40.4297],
  葫芦岛: [120.1575, 40.578],
  衡水: [115.8838, 37.7161],
  衢州: [118.6853, 28.8666],
  西宁: [101.4038, 36.8207],
  西安: [109.1162, 34.2004],
  贵阳: [106.6992, 26.7682],
  连云港: [119.1248, 34.552],
  邢台: [114.8071, 37.2821],
  邯郸: [114.4775, 36.535],
  郑州: [113.4668, 34.6234],
  鄂尔多斯: [108.9734, 39.2487],
  重庆: [107.7539, 30.1904],
  金华: [120.0037, 29.1028],
  铜川: [109.0393, 35.1947],
  银川: [106.3586, 38.1775],
  镇江: [119.4763, 31.9702],
  长春: [125.8154, 44.2584],
  长沙: [113.0823, 28.2568],
  长治: [112.8625, 36.4746],
  阳泉: [113.4778, 38.0951],
  青岛: [120.4651, 36.3373],
  韶关: [113.7964, 24.7028],
  香港: [114.16546, 22.27534]
}
const chinaGeoCoordMap = {
  黑龙江: [127.9688, 45.368],
  内蒙古: [110.3467, 41.4899],
  吉林: [125.8154, 44.2584],
  北京市: [116.4551, 40.2539],
  辽宁: [123.1238, 42.1216],
  河北: [114.4995, 38.1006],
  天津: [117.4219, 39.4189],
  山西: [112.3352, 37.9413],
  陕西: [109.1162, 34.2004],
  甘肃: [103.5901, 36.3043],
  宁夏: [106.3586, 38.1775],
  青海: [101.4038, 36.8207],
  新疆: [87.9236, 43.5883],
  西藏: [91.11, 29.97],
  四川: [103.9526, 30.7617],
  重庆: [108.384366, 30.439702],
  山东: [117.1582, 36.8701],
  河南: [113.4668, 34.6234],
  江苏: [118.8062, 31.9208],
  安徽: [117.29, 32.0581],
  湖北: [114.3896, 30.6628],
  浙江: [119.5313, 29.8773],
  福建: [119.4543, 25.9222],
  江西: [116.0046, 28.6633],
  湖南: [113.0823, 28.2568],
  贵州: [106.6992, 26.7682],
  云南: [102.9199, 25.4663],
  广东: [113.12244, 23.009505],
  广西: [108.479, 23.1152],
  海南: [110.3893, 19.8516],
  上海: [121.4648, 31.2891]
}
function getLine(data:any) {
    var res = [];
    for (var i = 0; i < data.length; i++) {
        var dataItem = data[i];
        var toCoord = geoCoordMap[dataItem.name];
        var fromCoord = [116.4551, 40.2539]; //中心点地理坐标
        if (fromCoord && toCoord) {
            res.push([
                {
                    // 飞线从哪里出发
                    coord: fromCoord,
                    value: dataItem.value,
                },
                {
                    // 飞线去往哪里
                    coord: toCoord,
                },
            ]);
        }
    }
    console.log('lines',data,res)
    return res;
}
var color = ['#438bf7', '#a6c84c', '#3ed4ff']
var series: any = []
const BQData = [

  {
    name: '北京',
    value: 110,
    power: 10
  },
  {
    name: '上海',
    value: 36,
    power: 10
  },
  {
    name: '深圳',
    value: 15,
    power: 10
  },
  {
    name: '成都',
    value: 9,
    power: 10
  },
  {
    name: '大连',
    value: 2
  },
  {
    name: '济南',
    value: 1
  },
  {
    name: '青岛',
    value: 2
  },
  {
    name: '哈尔滨',
    value: 1
  },
  {
    name: '南京',
    value: 1
  },
  {
    name: '海南',
    value: 2
  },
  {
    name: '杭州',
    value: 10
  },
  {
    name: '湖南',
    value: 2
  },
  {
    name: '江苏',
    value: 4
  },
  {
    name: '辽宁',
    value: 1
  },
  {
    name: '兰州',
    value: 1
  },
  {
    name: '天津',
    value: 8
  },
  {
    name: '西安',
    value: 4
  },
  {
    name: '石家庄',
    value: 1
  },
  {
    name: '武汉',
    value: 3
  },
  {
    name: '重庆',
    value: 2
  },
  {
    name: '吉林',
    value: 1
  },
  {
    name: '福建',
    value: 1
  },
  {
    name: '香港',
    value: 3
  },
]
  ;[
    ['XX信息', BQData]
    // ['西区', XQData]
  ].forEach(function (item: any, i) {
    series.push(
      // {
      //   name: item[0] + ' Top10',
      //   type: 'lines',
      //   zlevel: 1,
      //   effect: {
      //     show: true,
      //     period: 6,
      //     trailLength: 0.7,
      //     color: '#fff',
      //     symbolSize: 3
      //   },
      //   lineStyle: {
      //     normal: {
      //       color: color[i],
      //       width: 0,
      //       curveness: 0.2
      //     }
      //   },
      //   data: convertData(item[1])
      // },
      // {
      //   name: item[0] + ' Top10',
      //   type: 'lines',
      //   zlevel: 2,
      //   effect: {
      //     show: true,
      //     period: 6,
      //     trailLength: 0,
      //     symbol: planePath,
      //     symbolSize: 15
      //   },
      //   lineStyle: {
      //     normal: {
      //       color: color[i],
      //       width: 1,
      //       opacity: 0.4,
      //       curveness: 0.2
      //     }
      //   },
      //   data: convertData(item[1])
      // },
      // 设置飞线样式
      {
        type: "lines",
        coordinateSystem: "geo",
        zlevel: 100,
        effect: {
            show: true,
            period: 4, // 图标飞跃速度，值越小速度越快
            trailLength: 0.2, // 尾迹长度[0,1]值越大，尾迹越长
            symbol: "pin", // 图标类型
            symbolSize: 8, // 图标大小
            color: "#438bf7", // 图标颜色
        },
        lineStyle: {
            color: "#000",
            normal: {
                show: true,
                width: 2, //尾迹线条宽度
                opacity: 0.1, //尾迹线条透明度
                curveness: 0.3, //尾迹线条曲直度
                color: "#ec9241", // 飞线颜色
            },
        },
        // 飞线数据
        data: getLine(item[1]),
    },
      {
        name: item[0], //+ ' Top10'
        type: 'effectScatter',
        coordinateSystem: 'geo',
        zlevel: 2,
        rippleEffect: {
          brushType: 'stroke'
        },
        label: {
          normal: {
            show: true,
            position: 'right',
            formatter: '{b}'
          }
        },
        symbolSize: function (val) {
          return Math.min(val[2] * 2, 40) / 2
        },
        itemStyle: {
          normal: {
            color: color[i]
          }
        },
        data: item[1].map(function (dataItem) {
          let geoPos = dataItem.pos ? dataItem.pos : chinaGeoCoordMap[dataItem.name] || geoCoordMap[dataItem.name]
          if (!geoPos) {
            console.log(dataItem.name)
            throw new Error(dataItem.name + '没有经纬度')
          }
          console.log(geoPos.concat([dataItem.value]))
          return {
            name: dataItem.name,
            value: geoPos.concat([dataItem.value, dataItem.powe || 0])
          }
        })
      }
    )
  })
const ChinaMap: React.FC<ChinaMapProps> = ({ option, width = '100%', height = '100%' }) => {
  const divRef = useRef<HTMLDivElement>(null);
  const [transitionInterval, seTtransitionInterval] = useState<any>(null)
  const echartsRef = useRef<typeof ReactECharts>(null);
  // const chart = echarts.init(mapRef.current);
  const defaultOption = {
    // backgroundColor: '#101129',
    title: {
      left: 'left',
      textStyle: {
        color: '#fff'
      }
    },
    tooltip: {
      trigger: 'item',
      formatter: function (params, _ticket, _callback) {
        console.log(params, params.name, params.seriesIndex, params.value)
        //根据业务自己拓展要显示的内容
        var res = ''
        var value = Array.isArray(params.value) ? params.value[2] : params.value
        var power = Array.isArray(params.value) ? params.value[3] : params.value
        res =
          params.seriesName + '-' + params.name +
          "<br /><span style='margin-right:10px;color:#fff;display:inline-block;width:10px;height:10px;border-radius:10px;background-color:" +
          params.color +
          "'>" +
          '</span>机柜' +

          ':' +
          value
          + "<br /><span style='margin-right:10px;color:#fff;display:inline-block;width:10px;height:10px;border-radius:10px;background-color:" +
          params.color +
          "'>" +
          '</span>耗电量' +

          ':' +
          power + 'KW·h'

        console.log(res)
        return res
      }
    },
    legend: {
      show: false,
      orient: 'vertical',
      top: 'bottom',
      left: 'right',
      // data: ['北京 Top10', '上海 Top10', '广州 Top10'],
      textStyle: {
        color: '#fff'
      },
      selectedMode: 'single'
    },
    geo: {
      map: 'china',
      // zoom: 1.2,
      label: {
        emphasis: {
          show: false
        }
      },
      // roam: true,
      itemStyle: {
        normal: {
          // areaColor: '#142957',
          areaColor: '#f5f8fe',
          borderColor: '#438bf7'
        },
        emphasis: {
          areaColor: '#99ccff'
        }
      }
    },
    series
    // ...option
  };

  useEffect(() => {
    //MutationObserver 监测 divRef.current 容器变化
    console.log('divRef', divRef, echartsRef)
    const resizeFn = throttle((mutationsList) => {
      const e = new Event('resize', {
        bubbles: true,
        cancelable: true
      })
      console.log('divRef', mutationsList)
      document.dispatchEvent(e)
    }, 200)
    const observer = new ResizeObserver((mutationsList, observer) => {
      for (const entry of mutationsList) {
        if (entry.contentBoxSize) {
          // 使用节流
          // 节流函数
          // 执行echartsRef.current?.resize()
          // echartsRef.current?.resize()
          resizeFn(mutationsList)
        }
      }
    })
    observer.observe(echartsRef.current?.ele as HTMLElement)
    return () => {
      observer.disconnect()
    }
  }, [])
  return <ReactECharts className='xxl-flex-1' ref={echartsRef} option={defaultOption} />
  //  <div style={{transition:'height 0.2s linear, width 0.2s linear'}} ref={divRef}></div>
  //  </div>;
};

export default ChinaMap;